#include <iostream>
#include <assert.h>
namespace bit
{
    template<class T>
    class vector
    {
    public:
        // Vector的迭代器是一个原生指针
        typedef T* iterator;
        typedef const T* const_iterator;

        iterator begin() {
            return _start;
        }

        iterator end() {
            return _finish;
        }

        const_iterator cbegin() {
            return _start;
        }


        const_iterator cend() const{
            return _finish;
        }


        // construct and destroy
        vector()
            :_start(nullptr)
            ,_finish(nullptr)
            ,_endOfStorage(nullptr)
        {}

        vector(int n, const T& value = T())
            :_start(nullptr)
            , _finish(nullptr)
            , _endOfStorage(nullptr)
        {
            reserve(n);
            while(n--)
            {
                push_back(value);
            }
        }

        template<class InputIterator>
        vector(InputIterator first, InputIterator last) //判断插入
        {
            reserve(last - first);
            while (first != last)
            {
                push_back(*first);
                ++first;
            }
        }

        //v2(v1)
        vector(const vector<T>& v)
            : _start(nullptr), _finish(nullptr), _endOfStorage(nullptr)
        {
          /*  reserve(v.capacity);
            iterator it = begin();
            const iterator vt = v.cbegin();
            
            while (vt != v.cend()) {
                *it++ = *vt++;
            }
            _finish = _start + v.size();
            _endOfStorage = _start + v.capacity();*/

            reserve(v.capacity());
            for (auto e : v) push_back(e);
        }

        // v1 = v2
        vector<T>& operator= (vector<T> v)
        {
            swap(v);
            return *this;
        }

        ~vector()
        {
            delete[] _start;
            _start = _finish = _endOfStorage = nullptr;
        }

            // capacity
        size_t size() const
        {
            return _finish - _start;
        }

        size_t capacity() const
        {
            return _endOfStorage - _start;
        }

        void reserve(size_t n) //分空间
        {
            if (n > capacity())
            {
                size_t oldsize = size();
                T* tmp = new T[n];
                if (_start)
                {
                    for (size_t i = 0; i < oldsize; i++)
                    {
                        tmp[i] = _start[i];
                    }
                    delete[] _start;
                }
                _start = tmp;
                _finish = _start + oldsize;
                _endOfStorage = _start + n;
            }
        }

        void resize(size_t n, const T& value = T())
        {
            if (n <= size())
            {
                _finish = _start + n;
                return;
            }
            if (n > capacity()) reserve(n);
            iterator it = _finish;
            iterator _finish = _start + n;
            while (it != _finish)
            {
                *it = value;
                ++it;
            }
        }


            ///////////////access///////////////////////////////

        T& operator[](size_t pos){
            return _start[pos];
        }

        const T& operator[](size_t pos)const {
            return _start[pos];
        }


            ///////////////modify/////////////////////////////

        void push_back(const T& x) {
            insert(end(), x);
        }

        void pop_back() {
            erase(--end());
        }

        void swap(vector<T>& v)
        {
            std::swap(_start, v._start);
            std::swap(_finish, v._finish);
            std::swap(_endOfStorage, v._endOfStorage);

        }

        iterator insert(iterator pos, const T& x)
        {
            assert(pos <= _finish);

            if (_finish == _endOfStorage) {
                size_t size = pos - _start;
                size_t newCapacity = (0 == capacity() ? 4 : capacity() * 2);
                reserve(newCapacity);
               
                pos = _start + size;
            }
            //后移
            iterator end = _finish - 1; 
            while (end >= pos)
            {
                *(end + 1) = *end;
                --end;
            } 
            *pos = x;
            ++_finish;
            return pos;
        }

        iterator erase(iterator pos) { //前移
            iterator begin = pos + 1;
            while (begin!=_finish){
                *(begin - 1) = *begin;
                ++begin;
            }
            --_finish;
            return pos;
        }

    private:
        iterator _start; // 指向数据块的开始
        iterator _finish; // 指向有效数据的尾
        iterator _endOfStorage; // 指向存储容量的尾

    };

}



void test1()
{
    bit::vector<int> v1;
    v1.push_back(1);
    v1.push_back(2);
    v1.push_back(3);

    std::cout << "v1的遍历为：";
    for (auto e : v1) {
        std::cout << e << " ";
    }
    std::cout << std::endl;
    std::cout << "请输入你要插入在第2个位置的值" << " : ";
    int x;
    std::cin >> x;
    bit::vector<int>::iterator it;    
    it = v1.insert(v1.begin() + 1, x);
    for (auto e : v1)
    {
        std::cout << e << " ";
    }

    std::cout << std::endl;
    std::cout << "删除在第2个位置的值" << " : ";
    it = v1.erase(v1.begin() + 1);
    for (auto e : v1)
    {
        std::cout << e << " ";
    }
}

int main()
{
    test1();

	return 0;
}